package com.krt.game01;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;


public class MainActivity extends Activity {
	private static final boolean DRAWLINE = false;		//draw line when sliding finger
	public static final boolean NEGATIVEALLOWED = false;		//negative numbers (results) allowed 

	CustomDrawableView mCustomDrawableView;

	Level currentLevel;


	//--------- global variables ----------//
	boolean g_touchedDown = false;
	int g_width = 0;
	int g_height = 0;

	//dimensions of the View
	int g_viewHeight = 0;
	int g_viewWidth = 0;

	int g_slideStartX = 0;
	int g_slideStartY = 0;

	int g_slideEndX = 0;
	int g_slideEndY = 0;

	public static Bitmap g_bmp_button_grey = null;
	public static Bitmap g_bmp_button_green = null;


	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mCustomDrawableView = new CustomDrawableView(this);    	


		int[] locOnScreen = new int[2];
		mCustomDrawableView.getLocationOnScreen(locOnScreen);
		int viewYOffset = locOnScreen[1];
		g_viewHeight = mCustomDrawableView.getHeight();
		g_viewWidth = mCustomDrawableView.getWidth();
		Log.d("onCreate: viewHeight", String.valueOf(g_viewHeight));
		Log.d("onCreate: viewWidth", String.valueOf(g_viewWidth));
		Log.d("onCreate: viewYOffset", String.valueOf(viewYOffset));

		setContentView(mCustomDrawableView);

		g_bmp_button_grey = BitmapFactory.decodeResource(getResources(),
				R.drawable.button_grey);

		g_bmp_button_green = BitmapFactory.decodeResource(getResources(),
				R.drawable.button_green);

	}



	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch(item.getItemId())
		{
		case R.id.Level_1:
			Log.d("onOptionsItemSelected:", "Level 1 was selected");
			currentLevel = new Level(this, g_width, g_height, 
					2, 3, 4,
					5, 6, 7, 8,
					9, 10, 11, 12,
					13, 14, 15, 16);
			mCustomDrawableView.invalidate();
			return true;
		case R.id.Level_2:
			Log.d("onOptionsItemSelected:", "Level 2 was selected");
			currentLevel = new Level(this, g_width, g_height, 
					2, 3, 4,
					5, 6, 7, 8, 9);
			mCustomDrawableView.invalidate();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}


	public class CustomDrawableView extends View {

		public CustomDrawableView(Context context) {
			super(context);

		}



		//This method is used to initialize the level (width, height)
		protected void onSizeChanged (int w, int h, int oldw, int oldh){
			Log.d("onSizeChanged: h", String.valueOf(h));
			Log.d("onSizeChanged: w", String.valueOf(w));

			g_width = w;
			g_height = h;

		}



		protected void onDraw(Canvas canvas) {


			if (currentLevel != null) {
				for(int i=1; i<=currentLevel.getButtonList().size(); i++){
					if (currentLevel.getButtonList().get(i-1).isVisible() == true) {
						currentLevel.getButtonList().get(i-1).draw(canvas);
					}
				}
				currentLevel.m_plusButton.draw(canvas);
				currentLevel.m_minusButton.draw(canvas);
				currentLevel.m_chainButton.draw(canvas);
			}


			if (DRAWLINE) {	
				if (g_touchedDown=true) {
					Paint tempPaint = new Paint();
					tempPaint.setStyle(Paint.Style.STROKE);
					tempPaint.setColor(Color.BLACK);
					tempPaint.setStrokeWidth(3);				
					canvas.drawLine(g_slideStartX, g_slideStartY, g_slideEndX, g_slideEndY, tempPaint);
				}
			}
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		float touchX = event.getX();
		float touchY = event.getY();
		mCustomDrawableView.invalidate();

		//get offset for View (because there is a menu above our view!)
		//x = locOnScreen[0], y = locOnScreen[1]
		int viewYOffset;
		int[] locOnScreen = new int[2];
		mCustomDrawableView.getLocationOnScreen(locOnScreen);
		g_viewHeight = mCustomDrawableView.getHeight();
		g_viewWidth = mCustomDrawableView.getWidth();
//		Log.d("onTouchEvent: viewHeight", String.valueOf(g_viewHeight));
//		Log.d("onTouchEvent: viewWidth", String.valueOf(g_viewWidth));

		viewYOffset = locOnScreen[1];
//		Log.d("onTouchEvent: viewYOffset", String.valueOf(viewYOffset));
		//System.out.println("location: " + locOnScreen[0] + ":" + locOnScreen[1]);

		switch(event.getAction()){
		case MotionEvent.ACTION_DOWN:
//			Log.d("MotionEven.ACTION_DOWN", "Touching down!");
			g_slideStartX = (int)touchX;
			g_slideStartY = (int)touchY - viewYOffset;
			g_slideEndX = g_slideStartX;
			g_slideEndY = g_slideStartY;

			if (currentLevel != null) {

				
				//SPECIAL BUTTONS: touched?
				if (currentLevel.m_plusButton.contains((int)touchX, (int)touchY - viewYOffset) &&
						currentLevel.m_plusButton.isVisible() == true){
					if (currentLevel.m_plusButton.getTouched() == false) {
						currentLevel.m_plusButton.setTouched(true);
						currentLevel.m_minusButton.setTouched(false);
						currentLevel.m_chainButton.setTouched(false);
					}
					else if (currentLevel.m_plusButton.getTouched() == true) {
						currentLevel.m_plusButton.setTouched(false);
					}
				}

				if (currentLevel.m_minusButton.contains((int)touchX, (int)touchY - viewYOffset) &&
						currentLevel.m_minusButton.isVisible() == true){
					if (currentLevel.m_minusButton.getTouched() == false) {
						currentLevel.m_minusButton.setTouched(true);
						currentLevel.m_plusButton.setTouched(false);
						currentLevel.m_chainButton.setTouched(false);
					}
					else if (currentLevel.m_minusButton.getTouched() == true) {
						currentLevel.m_minusButton.setTouched(false);
					}
				}
				
				if (currentLevel.m_chainButton.contains((int)touchX, (int)touchY - viewYOffset) &&
						currentLevel.m_chainButton.isVisible() == true){
					if (currentLevel.m_chainButton.getTouched() == false) {
						currentLevel.m_chainButton.setTouched(true);
						currentLevel.m_plusButton.setTouched(false);
						currentLevel.m_minusButton.setTouched(false);
					}
					else if (currentLevel.m_chainButton.getTouched() == true) {
						currentLevel.m_chainButton.setTouched(false);
					}
				}	
				
				
				//iterate through ButtonList and check if Button was touched
				for(int i=1; i<=currentLevel.getButtonList().size(); i++){
		
						if (currentLevel.getButtonList().get(i-1).contains((int)touchX, (int)touchY - viewYOffset) &&		//number button touched?
								currentLevel.m_plusButton.getTouched() == false &&				//plus button was touched NOT before?
								currentLevel.m_minusButton.getTouched() == false &&				//minus button was touched NOT before?
								currentLevel.m_chainButton.getTouched() == false &&				//chain button was touched NOT before?
								currentLevel.m_buttonChain.size() == 0) {						//0 elements in button chain list?
							currentLevel.getButtonList().get(i-1).setTouched(true);
							currentLevel.m_buttonChain.add(currentLevel.getButtonList().get(i-1));
							Log.d("onTouchEvent", "adding first number button");
							
						}
						
						//plus button
						if (currentLevel.m_plusButton.getTouched() == true &&								//plus button touched?
								currentLevel.m_plusButton.isVisible() == true &&							//plus button visible?
								currentLevel.m_buttonChain.size() == 1) {									//only one element in button chain list?
							currentLevel.m_buttonChain.add(currentLevel.m_plusButton);
							Log.d("onTouchEvent", "adding plus button");
						}
						
						//minus button
						if (currentLevel.m_minusButton.getTouched() == true &&								//minus button touched?
								currentLevel.m_minusButton.isVisible() == true &&							//minus button visible?
								currentLevel.m_buttonChain.size() == 1) {									//only one element in button chain list?
							currentLevel.m_buttonChain.add(currentLevel.m_minusButton);
							Log.d("onTouchEvent", "adding minus button");
						}
						
								
						//number button after plus or minus button
						if (currentLevel.getButtonList().get(i-1).contains((int)touchX, (int)touchY - viewYOffset) &&						//number button touched?
								(currentLevel.m_plusButton.getTouched() == true ||								//plus button was touched before?
								currentLevel.m_minusButton.getTouched() == true) &&						//minus button was touched before?
								currentLevel.m_buttonChain.size() == 2) {										//two elements in button chain list? (we assume: number/plus/number
							currentLevel.getButtonList().get(i-1).setTouched(true);	
							currentLevel.m_buttonChain.add(currentLevel.getButtonList().get(i-1));
							Log.d("onTouchEvent", "adding second number button");
							currentLevel.checkCurrentChain();
						}					
						
						
						//TODO: Add code that checks if a special button was clicked FIRST --> which is not allowed
					}
				
			
				Log.d("onTouchEvent", "m_buttonChain.size = " + currentLevel.m_buttonChain.size());
				
			}

			break;
		case MotionEvent.ACTION_UP:
			Log.d("MotionEven.ACTION_UP", "Touching up!");
			g_touchedDown = false;

			g_slideEndX = g_slideStartX;
			g_slideEndY = g_slideStartY;

			break;
		case MotionEvent.ACTION_MOVE:
//			Log.d("MotionEvent.ACTION_MOVE", "Sliding your finger around on the screen.");

			g_slideEndX = (int)touchX;
			g_slideEndY = (int)touchY - viewYOffset;
			g_touchedDown = true;

			break;
		}
		return true;
	}


}
